<?php
class Treasurehunter_model  extends Base_Model
{
	public function __construct()
	{
		$this->load->database();
	}
	
	/**
	 *获取微信设备信息
	 *@return array
	 * */
	public function get_wx_device_params()
	{
		$query = $this->db->from('wx_device')->get()->result_array();
		foreach($query as &$v){
			$v['icon'] = $this->img($v['icon']);
			//生成随机浮点数
			$min = 0;
			$max = 1;
			$v['randomnum'] = $min + mt_rand() / mt_getrandmax() * ($max - $min);
			$v['randomnum'] = (string)$v['randomnum'];
		}
		return $query;
	}
	/**
	 *获取微信设备情报
	 *@param uuid string
	 *@param major int
	 *@param minor int
	 *@return array
	 * */
	public function get_wx_device_qb($uuid,$major,$minor)
	{
		$res = array('qb'=>'','bg'=>'');
		
		//获取设备id和所用于的活动id
		$query = $this->db->select('id,actId')->from('wx_device')
				->where('uuid',$uuid)
				->where('major',$major)
				->where('minor',$minor)
				->get()->row_array();
		
		if(!empty($query)){
			//获取该设备情报
			$qb = $this->db->from('wx_device_qingbao')
			->where('actId',$query['actId'])
			->where('device_id',$query['id'])
			->get()->result_array();
			//将情报type=1和背景type=2分组
			if(!empty($qb)){
				foreach ($qb as $v){
					//补充thumb路径
					$v['thumb'] = $this->img($v['thumb']);
					if($v['type'] == 1){
						$res['qb'][]=$v;
					} else {
						$res['bg'][]=$v;
					}
				}
			}
		}
		
		return $res;
	}
	
	/**
	 * 修改用户报名信息
	 *
	 * @param $data(array)
	 * @param $where(string/array)
	 * @return bool
	 */
	public function updateRegisterDetail($data, $where)
	{
		return $this->db->update('wx_activity_register', $data, $where);
	}
	
	/**
	 * 获取列表信息
	 *
	 * @param $offset(int,0)
	 * @param $limit(int,10)
	 * @param $where(string/array,array())
	 * @return array
	 */
	public function getList($offset = 0,$limit = 10,$where = array())
	{
		$this->db->from('mod_treasure_hunter');
		
		if(!empty($where)){
			$this->db->where($where);
		}
		$this->db->limit($limit,$offset);
		$this->db->order_by('deadline','ASC');
		$query = $this->db->get()->result_array();
		
		foreach ($query as &$v){
			$v['deadline'] = date('Y-m-d',$v['deadline']);
			$v['bgurl'] = $this->img($v['bgurl']);
		}
		
		if(!empty($query)){
			return $query;
		}else{
			return '';
		}
	}
	/**
	 * 获取活动信息
	 * @param $id
	 * @return array
	 */
	public function detail($id)
	{
	
		$this->db->where('id', $id);
		$this->db->from('mod_treasure_hunter');
		$query = $this->db->get()->row_array();
		if(!empty($query)){
			//分割图片地址
			$query['images']=trim($query['images']);
			$query['images']=rtrim($query['images'],'|');
			$query['images'] = explode('|',$query['images']);
			foreach ($query['images'] as &$v){
				$v=$this->img($v);
			}
			//活动须知换行
			$query['note']=rtrim($query['note'],'|');
			$query['note'] = explode('|', $query['note']);
			foreach ($query['note'] as &$v){
				$v=trim($v);
			}
			//已预订的人数
			$this->db->where('actId',$id);
			$this->db->where('issueNum',$query['issueNum']);
			$this->db->where('status','PAID');
			$this->db->from('wx_activity_register');
			$query['orders']=$this->db->count_all_results();
			//价格
			$query['price']/=100;
			$query['priceGroup']/=100;

			return $query;
		} else {
			return false;
		}
	}
	/**
	 * 获取用户活动报名信息
	 * @param $where(array/string)
	 * @return array
	 */
	public function registerDetail($where)
	{
		$this->db->where($where);
		$this->db->from('wx_activity_register');
		$query = $this->db->get()->row_array();
		
		if(!empty($query)){
			return $query;
		} else {
			return '';
		}
	}
	/**
	 * 判断用户是否已经报名
	 * @param $uid,$actId,$issueNum
	 * @return string
	 */
	public function checkUserisRegistered($uid,$actId,$issueNum)
	{
		$this->db->where('uid',$uid);
		$this->db->where('actId',$actId);
		$this->db->where('issueNum',$issueNum);
		$this->db->from('wx_activity_register');
		$query = $this->db->get()->row_array();
		
		$res['type'] = $query['type'];
		$res['inviNum'] = $query['inviNum'];
		
		if (!empty($query)) {
			if($query['status'] == 'PAID'){
				$res['status'] = 'PAID';
			} else {
				$res['status'] = 'UNPAID';
			} 
		} else {
			$res['status'] = 'UNREGISTERED';
		}
		
		return $res;
	}
	
	/**
	 * 判断组团口令是否存在,并返回团队成员
	 * @param $actId,$issueNum,$grpcode
	 * @return mixed
	 * */
	public function checkGrpcodeisExist($actId,$issueNum,$grpcode,$uid)
	{
		$this->db->select('r.uid,r.name,u.userIcon');
		$this->db->from('wx_activity_register as r');
		$this->db->join('user_v2 as u','r.uid=u.uid','left');
		$this->db->where('r.actId',$actId);
		$this->db->where('r.issueNum',$issueNum);
		$this->db->where('r.inviNum',$grpcode);		
		$query = $this->db->get()->result_array();
		
		//判断该用户是否属于这组，同时判断是否付款
		$status = $this->db->select('status')->from('wx_activity_register')
		              ->where('uid',$uid)
		              ->where('actId',$actId)
		              ->where('issueNum',$issueNum)
		              ->where('inviNum',$grpcode)
		              ->get()->row_array();
		
		if(empty($status)){
			$userStatus = '';
		} else {
			$userStatus = $status['status'];
		}
		
		if(!empty($query)){
			$res = array('group'=>$query,'userStatus'=>$userStatus);
			return $res;
		}else{
			return '';
		}
	}
	/**
	 * @param $data
	 * @return mixed
	 * 用户报名
	 */
	public function userRegister($data){
		if($this->db->insert('wx_activity_register', $data)){
			return $this->db->insert_id();
		}else{
			return false;
		}
	}
}